﻿using NaireSystem.Api;
using NaireSystem.Datas;
using NaireSystem.DBEntity;
using NaireSystem.Enums;
using NaireSystem.ViewModels;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using TYBase;
using TYControls;

namespace NaireSystem.Views.UserManager
{
    /// <summary>
    /// ViewUserInfoEdit.xaml 的交互逻辑
    /// </summary>
    public partial class ViewUserInfoEdit : TYWindow
    {
        ViewModelEditUserInfo vm = new ViewModelEditUserInfo();
        public ViewUserInfoEdit(UserInfoModel dx)
        {
            InitializeComponent();
            UserInfo = dx;
            this.DataContext = vm;
            this.Loaded += ViewUserInfoEdit_Loaded;
        }

        private void ViewUserInfoEdit_Loaded(object sender, RoutedEventArgs e)
        {
            InitList(EnumDictType.user_education, vm.ListEducation);
            InitList(EnumDictType.user_major, vm.ListMajor);
            InitList(EnumDictType.user_level, vm.ListLevel);
            InitList(EnumDictType.user_title, vm.ListTitle);
            if (UserInfo != null && UserInfo.Data != null)
            {
                vm.UserId = UserInfo.Data.user_id;
                vm.UserName = UserInfo.Data.user_name;
                vm.WorkYear = UserInfo.Data.user_work_year;
                vm.Age = UserInfo.Data.user_age.ToString();

                vm.StartWorkTime = UserInfo.Data.user_start_work_date;
                //if (UserInfo.Data.user_start_work_date < DateTime.Now.AddYears(-100))
                //{
                //    vm.StartWorkTime = null;
                //}
                vm.SelectedEducation = vm.ListEducation.FirstOrDefault(x => x.Key == UserInfo.Data.user_education_key);
                vm.SelectedMajor = vm.ListMajor.FirstOrDefault(x => x.Key == UserInfo.Data.user_major_key);
                vm.SelectedTitle = vm.ListTitle.FirstOrDefault(x => x.Key == UserInfo.Data.user_title_key);
                vm.SelectedLevel = vm.ListLevel.FirstOrDefault(x => x.Key == UserInfo.Data.user_level_key);
            }
        }

        void InitList(EnumDictType enumDict, ObservableCollection<IntStringData> list)
        {
            list.Clear();
            var listTmp = AppDictData.GetList(enumDict);
            if (listTmp != null)
                foreach (var item in listTmp)
                {
                    list.Add(new IntStringData { Key = item.Key, Value = item.Value });
                }

        }

        public UserInfoModel UserInfo { get; }

        private async void BtnClick_OK(object sender, RoutedEventArgs e)
        {
            Tbl_UserInfo userInfo = new Tbl_UserInfo();
            if (vm.Age.AsInt() <= 0 || vm.Age.AsInt() > 150)
            {
                this.ShowMsg("年龄不符合!");
                return;
            }
            if (vm.StartWorkTime == null)
            {
                this.ShowMsg("请输入开始工作时间!");
                return;
            }
            userInfo.user_start_work_date = vm.StartWorkTime;
            userInfo.user_work_year = vm.WorkYear;
            userInfo.user_education_key = vm.SelectedEducation.Key;
            userInfo.user_major_key = vm.SelectedMajor.Key;
            userInfo.user_title_key = vm.SelectedTitle.Key;
            userInfo.user_level_key = vm.SelectedLevel.Key;
            userInfo.user_id = UserInfo.Data.user_id;
            userInfo.user_info_id = UserInfo.Data.user_info_id;
            userInfo.user_age = vm.Age.AsInt();
            var rData = await Run(() =>
            {
                return UserApi.Edit(userInfo);
            });
            if (rData.code != 0)
            {
                this.ShowMsg(rData.msg);
                return;
            }
        }
    }
}
